<template>
  <div v-if="visible" class="login-modal">
    <div class="login-layout">
      <!-- 左侧系统信息区域 -->
      <div class="system-info">
        <div class="logo-area">
          <img src="@/assets/logo.svg" alt="系统Logo" class="logo">
          <h1>图像管理系统</h1>
          <p>Image Management System</p>
        </div>
        <div class="system-features">
          <div class="feature-item">
            <i class="feature-icon">📁</i>
            <span>高效的图像管理</span>
          </div>
          <div class="feature-item">
            <i class="feature-icon">🔍</i>
            <span>智能图像分析</span>
          </div>
          <div class="feature-item">
            <i class="feature-icon">🔄</i>
            <span>便捷的批量处理</span>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单区域 -->
      <div class="login-form-container">
        <div class="login-form">
          <h2>用户登录</h2>
          <form @submit.prevent="handleSubmit">
            <div class="form-group">
              <label for="username">用户名</label>
              <input 
                type="text" 
                id="username"
                v-model="username"
                required
                placeholder="请输入用户名 (默认: admin)"
              >
            </div>
            
            <div class="form-group">
              <label for="password">密码</label>
              <input 
                type="password" 
                id="password"
                v-model="password"
                required
                placeholder="请输入密码 (默认: admin)"
              >
            </div>

            <div class="error-message" v-if="errorMsg">{{ errorMsg }}</div>
            
            <button 
              type="submit" 
              :disabled="isLoading"
              class="login-button"
            >
              {{ isLoading ? '登录中...' : '登录' }}
            </button>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'

const props = defineProps({
  visible: {
    type: Boolean,
    required: true
  }
})

const emit = defineEmits(['login-success', 'login-failed'])

const username = ref('')
const password = ref('')
const errorMsg = ref('')
const isLoading = ref(false)

const handleSubmit = async () => {
  try {
    isLoading.value = true
    errorMsg.value = ''
    
    console.log('Attempting login with:', {
      username: username.value,
      password: password.value
    })
    
    const response = await axios.post('http://111.229.88.2:8000/api/login', {
      username: username.value,
      password: password.value
    })
    
    console.log('Login response:', response.data)
    
    // 保存登录信息
    localStorage.setItem('token', response.data.token)
    localStorage.setItem('username', response.data.username)
    localStorage.setItem('userId', response.data.userId)
    
    // 设置axios默认headers
    axios.defaults.headers.common['Authorization'] = `Bearer ${response.data.token}`
    
    emit('login-success', response.data)
  } catch (error) {
    console.error('Login error:', error.response || error)
    errorMsg.value = '用户名或密码错误'
    emit('login-failed', errorMsg.value)
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background-color: #f0f2f5;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.login-layout {
  display: flex;
  width: 1000px;
  height: 600px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.system-info {
  flex: 1;
  background: linear-gradient(135deg, #1890ff, #096dd9);
  color: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.logo-area {
  text-align: center;
  margin-bottom: 60px;
}

.logo {
  width: 100px;
  height: 100px;
  margin-bottom: 20px;
}

.logo-area h1 {
  font-size: 28px;
  margin-bottom: 10px;
}

.logo-area p {
  font-size: 16px;
  opacity: 0.8;
}

.system-features {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 30px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 15px;
  font-size: 18px;
}

.feature-icon {
  font-size: 24px;
}

.login-form-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-form {
  width: 100%;
  max-width: 360px;
}

.login-form h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 40px;
  text-align: center;
}

.form-group {
  margin-bottom: 24px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-size: 14px;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.3s;
}

input:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.error-message {
  color: #ff4d4f;
  margin-bottom: 16px;
  font-size: 14px;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.login-button:hover {
  background-color: #096dd9;
}

.login-button:disabled {
  background-color: #d9d9d9;
  cursor: not-allowed;
}

@media (max-width: 1024px) {
  .login-layout {
    width: 90%;
    height: auto;
    flex-direction: column;
  }

  .system-info {
    padding: 20px;
  }

  .system-features {
    display: none;
  }
}
</style> 